package com.swak.metrics.dubbo;

import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_THREAD_NAME;
import static org.apache.dubbo.common.constants.CommonConstants.THREADS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.THREAD_NAME_KEY;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.threadpool.ThreadPool;

import com.swak.reactivex.threads.Contexts;

/**
 * 对其他的线程池进行 Wrap 提供线程池监控的功能
 * 
 * @author DELL
 */
@Activate
public class MetricsThreadPool implements ThreadPool {

	final ThreadPool pool;

	public MetricsThreadPool(ThreadPool pool) {
		this.pool = pool;
	}

	@Override
	public Executor getExecutor(URL url) {
		String name = url.getParameter(THREAD_NAME_KEY, DEFAULT_THREAD_NAME);
		int threads = url.getParameter(THREADS_KEY, Integer.MAX_VALUE);
		return Contexts.createMetricsContext(name, threads, (ExecutorService) pool.getExecutor(url));
	}
}